package util;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


/**
 * 数据库连接工具类
 */
public class  DbUtil{


    //数据库连接对象
    private Connection connection;
    //预状态通道
    private PreparedStatement pps;
    //查询的sql得到的结果集合
    private ResultSet resultSet;
    //执行sql的影响行数
    private int rows;

    //连接地址
    private static String url;
    //驱动
    private static String driver;
    //登入信息
    //用户名
    private static String userName;
    //密码
    private static String passWord;

    //德鲁伊连接池对象
    private static DruidDataSource druidDataSource = new DruidDataSource();


    //加载驱动
    static {

        //读取db.properties文件里的登录连接信息
        //ResourceBundle bundle = ResourceBundle.getBundle("db");
        //driver = bundle.getString("driverClassName");
        //url = bundle.getString("url");
        //userName = bundle.getString("userName");
        //passWord = bundle.getString("passWord");

        //将驱动、连接地址、数据库登录信息等参数给德鲁伊连接池
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/demo1?characterEncoding=utf-8&serverTimezone=GMT%2B8");
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setInitialSize(4);

    }

    //获得连接
    protected Connection getConnection(){
        try {
            connection = druidDataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    //得到预状态通道
    protected PreparedStatement getPps(String sql){
        try {
            pps = getConnection().prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return pps;
    }

    //绑定参数  list保存的是给占位符所赋的值
    protected void param(List list){
        if (list!=null && list.size()>0){
            for(int i=0;i<list.size();i++){
                try {
                    pps.setObject(i+1,list.get(i));
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    //执行（增、删、改）操作,获得影响行数
    protected int update(String sql,List list){
        getPps(sql);
        param(list);

        try {
            rows = pps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rows;
    }


    //执行查询操作，获得结果集合
    protected ResultSet query(String sql,List list){
        getPps(sql);
        param(list);

        try {
            resultSet = pps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }


    //query()方法的重载
    //执行全表查询操作,不传占位符参数
    protected ResultSet query(String sql){
        getPps(sql);
        try {
            resultSet = pps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }

    //关闭资源
    protected void closeAll(){
        try {
            if (resultSet != null){
                //关闭查询语句
                resultSet.close();
            }
            if (pps != null){
                //关闭sql通道
                pps.close();
            }
            if (connection != null){
                //关闭数据库连接
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
